﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using ListViewDragDrop.ServiceProviders.UI;
using CustomControl;
using ListViewDragDrop.Controls.Utilities;
using ListViewDragDrop.ServiceProviders.UI;
//using TeacherLeftHand.Frameworks.DragDropFrameWork;
//using TeacherLeftHand.Frameworks.DragDropFrameWorkData;
using TeacherLeftHand.Util;

namespace TeacherLeftHand.View
{
    /// <summary>
    /// Interaction logic for ActivityView.xaml
    /// </summary>
    public partial class ActivityView : UserControl
    {
        private ListView listParent;
        public static RoutedEvent ActivityChangedEvent = EventManager.RegisterRoutedEvent("ChangedEvent", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(ActivityView));
        //public bool listChanged = false;
        public ActivityView()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ActivityView_Loaded);
           // this.LayoutUpdated += new EventHandler(ActivityView_LayoutUpdated);

            //#region Register DropManager

            //// Data Consumer
            //var canvasButtonDataConsumer =
            //    new PanelDataConsumer<Panel, ContentControl>(new string[] { "CanvasButtonObject" });
            //// Drop Managers
            //var dropHelperCanvas0 = new DropManager(stackPanelActivityView,
            //    new IDataConsumer[] {  
            //        canvasButtonDataConsumer,
            //    });

            //#endregion
            var teachingMethods = new ObservableCollection<RichTextEditor>();
            this.listOfTeachingMethod.ItemsSource = teachingMethods;
            var dragdrop = new ListViewDragDropManager<RichTextEditor>(this.listOfTeachingMethod, 2);
        }

     

        void ActivityView_Loaded(object sender, RoutedEventArgs e)
        {
            listParent = Utility.TryFindParent<ListView>(this);
            var activityItemSource = this.listParent.ItemsSource as ObservableCollection<ActivityView>;
            if (activityItemSource != null)
            {
                activityItemSource.CollectionChanged += activities_CollectionChanged;
            }

            var teachingMethodItemSource = this.listOfTeachingMethod.ItemsSource as ObservableCollection<RichTextEditor>;
            if (teachingMethodItemSource != null)
            {
                teachingMethodItemSource.CollectionChanged += teachingMethods_CollectionChanged;
            }
        }

       

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            if (listParent != null)
            {
                IEditableCollectionView items = listParent.Items; //Cast to interface
                if (items.CanRemove)
                {
                    items.Remove(this);
                    items.CommitEdit();
                }
           
            }
            
            //int index = -1;


            //for (int i = 0; i < listParent.Items.Count; ++i)
            //{
            //    ListViewItem item = this.GetListViewItem(i, listParent);
            //    if (item != null)
            //    {
            //        if (this.IsMouseOver(item))
            //        {
            //            index = i;
            //            break;
            //        }
            //    }
            //}
            //if (index > 0)
            //{
            //    IEditableCollectionView items = listParent.Items; //Cast to interface
            //    if (items.CanRemove)
            //    {
            //        items.Remove(index);
            //    }

            //}
            //var parent = (Panel)this.Parent;
            //var parent2 = ((Button) sender).Parent;
            //parent.Children.Remove(this);
        }

        private void listViewContextMenu_Delete_Click(object sender, RoutedEventArgs e)
        {
            if (listOfTeachingMethod != null && listOfTeachingMethod.Items.Count>0)
            {
                IEditableCollectionView items = listOfTeachingMethod.Items; //Cast to interface
                if (items.CanRemove)
                {
                    items.Remove(listOfTeachingMethod.SelectedItem);
                    items.CommitEdit();
                }
            }
        }

        private void richTextEditor_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(ActivityChangedEvent));
        }

        void teachingMethods_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            //if(listChanged)
            //{
            this.RaiseEvent(new RoutedEventArgs(ActivityChangedEvent));
            //}else
            //{
            //    listChanged = true;
            //}
        }
        private void activities_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(ActivityChangedEvent));
        }

   
        //ListViewItem GetListViewItem(int index, ListView listParent)
        //{
        //    if (listParent.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
        //        return null;

        //    return listParent.ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;
        //}
        //bool IsMouseOver(Visual target)
        //{
        //    // We need to use MouseUtilities to figure out the cursor
        //    // coordinates because, during a drag-drop operation, the WPF
        //    // mechanisms for getting the coordinates behave strangely.

        //    Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
        //    Point mousePos = MouseUtilities.GetMousePosition(target);
        //    return bounds.Contains(mousePos);
        //}
    }
}
